package com.china08.yxyapi.api.manage.tec.teaching.lecture.teachreport;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.esbag.testreport.YxyEsbagTeachingTestAnalyseKnow;
import com.china08.yxyapi.entity.mg.esbag.testreport.YxyEsbagTeachingTestAnalyseKnow.Know;
import com.china08.yxyapi.entity.mg.esbag.testreport.YxyEsbagTeachingTestAnalyseScore;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingTestAnalyseKnowRepository;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingTestAnalyseScoreRepository;

/**
 * 老师 ---备授课--授课报告-分析
 * 
 */
@CrossOrigin
@RestController
public class LectureReportClassAnalyzeController extends YxyApiBaseController {

    @Autowired
    private YxyEsbagTeachingTestAnalyseKnowRepository yxyEsbagTeachingTestAnalyseKnowRepository;

    @Autowired
    private YxyEsbagTeachingTestAnalyseScoreRepository yxyEsbagTeachingTestAnalyseScoreRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/analyze/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String id = reqModel.id;
        Assert.notNull(StringUtils.defaultIfBlank(id, null), "授课包ID为空");
        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        String classId = reqModel.classId;
        RespModel respModel = new RespModel();
        YxyEsbagTeachingTestAnalyseKnow analyseKnow = yxyEsbagTeachingTestAnalyseKnowRepository
                .findByTeacherIdAndSchoolIdAndIdAndClassId(userId, schoolId, id, classId);

        YxyEsbagTeachingTestAnalyseScore analyseScore = yxyEsbagTeachingTestAnalyseScoreRepository
                .findByTeacherIdAndSchoolIdAndIdAndClassId(userId, schoolId, id, classId);
        if (null != analyseKnow) {
            Knows knows = new Knows();
            List<Know> knowsList = new ArrayList<Know>();
            Double rate = 60.0;
            BigDecimal data = new BigDecimal(rate);
            List<String> weakKnowsList = new ArrayList<String>();
            List<Know> list = analyseKnow.getKnows();

            // 知识点掌握统计
            for (Know know : list) {
                Double conparerate = know.rate;
                BigDecimal data2 = new BigDecimal(conparerate);
                // 掌握程度低于60%
                if (data.compareTo(data2) == 1) {
                    weakKnowsList.add(know.knowName);
                }
                knowsList.add(know);
            }
            knows.weakKnowsList = weakKnowsList;
            knows.knowsList = knowsList;
            respModel.knows = knows;
        }
        if (null != analyseScore) {
            // 正确率统计
            Map<Integer, Integer> scoreRate = analyseScore.getScoreRate();
            // key: -1--未缴 60--小于60% ; 79--60%-79% ; 89--80%-89% ; 99--90%-99% ;
            for (Integer key : scoreRate.keySet()) {
                if (-1 == key) {
                    respModel.overdue = scoreRate.get(key);
                } else if (60 == key) {
                    respModel.sixty = scoreRate.get(key);
                } else if (79 == key) {
                    respModel.seventy = scoreRate.get(key);
                } else if (89 == key) {
                    respModel.eighty = scoreRate.get(key);
                } else if (99 == key) {
                    respModel.ninety = scoreRate.get(key);
                } else if (100 == key) {
                    respModel.hundred = scoreRate.get(key);
                }
            }
            respModel.classRate = analyseScore.getClassRate();
            respModel.packName = analyseKnow.getPackName();
            respModel.userTime = analyseKnow.getUserTime();
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 授课包id
        public String id;
        // 班级ID
        public String classId;
    }

    protected class Knows {
        // 薄弱知识点
        public List<String> weakKnowsList;
        // 知识点
        public List<Know> knowsList;

    }

    protected static class RespModel extends YxyApiRespModel {
        // 授课包名称
        public String packName;
        // 授课时长
        public Long userTime;
        // 知识点
        public Knows knows;
        // 正确率100%人数
        public Integer hundred;
        // 正确率90-99%人数
        public Integer ninety;
        // 正确率80-89%人数
        public Integer eighty;
        // 正确率60-79%人数
        public Integer seventy;
        // 正确率小于60%人数
        public Integer sixty;
        // 未交人数
        public Integer overdue;
        // 总评分
        public Double classRate;
    }
}
